package jsdb

import (
	"encoding/json"
	"log"
	"os"
	"reflect"
	"time"
)

// 用户信息结构
type userInfo struct {
	Username string `json:"username"`
	Password string `json:"password"`
	CreateAt string `json:"create_at"`
	UpdateAt string `json:"update_at"`
}

// 数据库存储结构
type localData struct {
	Version string
	Data    []userInfo
	Count   int64
}

func checkErr(err error) {
	if err != nil {
		// fmt.Println(err)
		panic(err)
	}
}

const dataFile string = "./user_data.json"

func checkDataFile() (bool, error) {
	_, err := os.Stat(dataFile)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func GetReadUserData(username string, password string) (localData, bool, userInfo) {
	_data, err := os.ReadFile(dataFile)
	checkErr(err)
	newData := &localData{}
	err = json.Unmarshal(_data, &newData)
	checkErr(err)
	log.Println("数据版本: ", newData.Version)
	log.Println("用户统计: ", newData.Count)
	newUserInfo := &userInfo{}
	tag := false // 用户是否存在标记
	for num, v := range newData.Data {
		// fmt.Println("UserName: ", v.UserName)
		// 判断用户是否存在
		if username == v.Username {
			newUserInfo.Username = v.Username
			if password == v.Password {
				newUserInfo.Password = v.Password
				newUserInfo.UpdateAt = v.UpdateAt
			} else {
				newUserInfo.Password = password
				newUserInfo.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
			}
			newUserInfo.CreateAt = v.CreateAt

			// 修改用户信息
			newData.Data[num] = *newUserInfo

			// 修改 tag 的赋值
			newTag := reflect.ValueOf(&tag).Elem()
			newTag.SetBool(true)
		}
	}
	// fmt.Println("1212", tag)
	// fmt.Println(newData)
	return *newData, tag, *newUserInfo
}

func GetWriteUserData(username string, password string) userInfo {
	_userInfo := &userInfo{
		Username: username,
		Password: password,
		CreateAt: time.Now().Format("2006-01-02 15:04:05"),
		UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
	}
	// fmt.Println(_userInfo)
	// fmt.Println(_userInfo.CreateAt)
	_checkDataFele, err := checkDataFile()
	newData := &localData{}
	if _checkDataFele {
		newData, tag, _ := GetReadUserData(username, password)
		// log.Println("1111", newData, tag, userInfo)
		// 用户不存在则创建，否则更新用户信息
		if !tag {
			// log.Println("333", newData)
			newData.Data = append(newData.Data, *_userInfo)
			newData.Count = int64(len(newData.Data))
			data, err := json.MarshalIndent(newData, "", "	")
			checkErr(err)
			err = os.WriteFile(dataFile, data, 0777)
			checkErr(err)

		} else {
			// log.Println("555", newData)
			newData.Count = int64(len(newData.Data))
			data, err := json.MarshalIndent(newData, "", "	")
			checkErr(err)
			err = os.WriteFile(dataFile, data, 0777)
			checkErr(err)
		}
	} else {
		log.Println(err)
		newData.Version = "v1.0.0"
		newData.Count = 1
		newData.Data = []userInfo{*_userInfo}
		data, err := json.MarshalIndent(newData, "", "	")
		checkErr(err)
		err = os.WriteFile(dataFile, data, 0777)
		checkErr(err)
		// fmt.Println(newData)
	}

	// fmt.Println(_userInfo)
	return *_userInfo
}

// func main() {
// 	sa := GetwriteUserData("dahea")
// 	fmt.Println(sa)
// }

// func init() {}
